package ws.jdbc.concurrent.driver.sqlserver

/**
 * Test of scrollable and read-only result sets.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class ScrollableReadOnlyTest extends ForwardOnlyReadOnlyTest {

  /**
   * Test of previous() method.
   */
  void testPrevious(rs = rs) {
    println "testPrevious()"

    rs.afterLast()

    int count = 0
    while(rs.previous()) {
      assertLineValuesMatch rs, DataTest.table.size() - count
      ++count
    }
    assertEquals DataTest.table.size(), count
  }

  /**
   * Test of absolute() method.
   */
  void testAbsolute(rs = rs) {
    println "testAbsolute()"

    1.upto(10) {
      assertTrue rs.absolute(it)
      assertLineValuesMatch rs, it
    }

    rs.absolute(34)
    assertLineValuesMatch rs, 34

    while(rs.next()) {

    }

    rs.absolute(1)
    assertLineValuesMatch rs, 1

    rs.absolute(0) // this should put the curosr before fist row.
    rs.next()
    assertLineValuesMatch rs, 1

    rs.absolute(1000) // this should put the cursor after last row (I'm assuming that the table has less than 1000 rows).
    rs.previous()
    assertLineValuesMatch rs, DataTest.table.size()

    rs.absolute(-1)
    assertLineValuesMatch rs, DataTest.table.size()
    rs.absolute(-2)
    assertLineValuesMatch rs, DataTest.table.size() - 1
    rs.absolute(-20)
    assertLineValuesMatch rs,DataTest.table.size() - 19
  }

  /**
   * Test of relative() method.
   */
  void testRelative(rs = rs) {
    println "testRelative()"

    rs.relative(1)
    assertLineValuesMatch rs, 1

    rs.relative(3)
    assertLineValuesMatch rs, 4

    rs.relative(0)  // it shouldn't change the cursor position
    assertLineValuesMatch rs, 4

    rs.relative(40)
    assertLineValuesMatch rs, 44

    rs.relative(-4)
    assertLineValuesMatch rs, 40

    rs.relative(-30)
    assertLineValuesMatch rs, 10

    rs.relative(1000) // it should position the cursor after the last row
    assertTrue "The cursor should be after the last row.", rs.isAfterLast()

    rs.relative(-1)
    assertTrue rs.isLast()
    assertLineValuesMatch rs, DataTest.table.size()

    rs.relative(-1000) // it should position the cursor before the first row
    assertTrue "The cursor should be before the first row.", rs.isBeforeFirst()

    rs.relative(1)
    assertTrue rs.isFirst()
    assertLineValuesMatch rs, 1
  }

  /**
   * Test of first() method.
   */
  void testFirst(rs = rs) {
    println "testFirst()"

    rs.first()
    assertTrue rs.isFirst()
    assertLineValuesMatch rs, 1
  }

  /**
   * Test of beforeFirst()
   */
  void testBeforeFirst(rs = rs) {
    println "testBeforeFirst()"

    rs.absolute(9)
    assertLineValuesMatch rs, 9

    rs.beforeFirst()
    assertTrue rs.isBeforeFirst()

    assertResultSetEquals(rs) {
      rs.next()
    }
  }

   /**
   * Test of last() method.
   */
  void testLast(rs = rs) {
    println "testLast()"

    rs.last()
    assertTrue rs.isLast()
    assertLineValuesMatch rs, DataTest.table.size()

    rs.relative(-2)
    assertLineValuesMatch rs, DataTest.table.size() - 2
  }

  /**
   * Test of afterLast()
   */
  void testAfterLast(rs = rs) {
    println "testAfterLast()"

    rs.absolute(9)

    rs.afterLast()
    assertTrue rs.isAfterLast()

    assertResultSetEquals(DataTest.table.size(), false, rs) {
      rs.previous()
    }
  }
}